#!/bin/bash

# c_name=ab_workflow
# jar_name=ab-workflow-1.0.jar

if [ $# -lt 2 ]; then
    echo "usage: $0 container_name jar_name"
    echo "example: $0 workflow ab-workflow-1.0.jar"
    exit 1
fi
c_name=$1
jar_name=$2

################ git pull ################
echo -e "\n step0: git pull"
git pull
code=$?
if [ ! $code -eq 0 ];then
	echo "fail: git pull"
	exit $?
fi
sleep 1


################ maven package ################
echo -e "\n step1: maven package"
docker run -it --rm --name mvn_1 -v "$PWD":/usr/src/mymaven -w /usr/src/mymaven maven:3.3.3-jdk-8 \
mvn package -Dmaven.test.skip=true
code=$?
if [ ! $code -eq 0 ];then
	echo "fail: mvn package"
	exit $?
fi
sleep 1


################ run java app ################
echo -e "\n step2: run java app"
#docker run -d --net=host --name $c_name -v "$PWD":/usr/src/myapp -w /usr/src/myapp java:latest java -Djava.security.egd=file:/dev/./urandom -Ddubbo.properties.file=dubbo-prod.properties  -spring.profiles.active=prod -jar /usr/src/myapp/target/$jar_name

# remove old container
docker rm -f $c_name &> /dev/null


docker run -d \
--net=host \
--name $c_name \
-v $PWD:/usr/src/myapp \
-w /usr/src/myapp \
java:latest \
java \
-Djava.security.egd=file:/dev/./urandom \
-Ddubbo.properties.file=dubbo.properties \
-jar /usr/src/myapp/target/$jar_name \
-spring.profiles.active=dev 

code=$?
if [ ! $code -eq 0 ];then
	echo "run java fail"
	exit $?
fi
